package reactor;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
/**
 * @Author: linKe
 * @Date: 2022/1/15 15:07
 * @FileName: SocketReadHandler
 * @version: 1.0
 * @Description: 套接字读取处理程序
 */
public class SocketReadHandler implements Runnable {
    /**
     * 套接字管道对象
     */
    private SocketChannel socketChannel;

    public SocketReadHandler(Selector selector, SocketChannel socketChannel) throws IOException {
        this.socketChannel = socketChannel;
        socketChannel.configureBlocking(false);
        socketChannel.register(selector, SelectionKey.OP_READ, this);
        selector.wakeup();
    }

    @Override
    public void run() {
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        try {
            socketChannel.read(buffer);
            //后续处理...
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
